
package dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionFactory {

    private static Connection connection;

    public static Connection getConnection() {

        if (connection == null) {
            Properties properties = new Properties();
            try {
                File file =  new File("src\\properties\\bd.properties");
                FileInputStream stream = new FileInputStream(file);
                properties.load(stream);
                String driver = properties.getProperty("connection.driver_class");
                String url = properties.getProperty("connection.url");
                String user = properties.getProperty("connection.username");
                String password = properties.getProperty("connection.password");
                Class.forName(driver).newInstance();
                connection = DriverManager.getConnection(url, user, password);
            } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }
}
